<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="uploadCertUrl"
      :headers="headers"
      :accept="configSet.acceptList"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload"
      :multiple="configSet.multiple"
      :limit="configSet.limit"
      :on-exceed="handleExceed"
      :file-list="fileList"
      :data="extraData"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip" v-if="configSet.showTip">
        上传文件大小不能超过{{configSet.limitSize}}MB
      </div>
    </el-upload>
  </div>
</template>

<script>
import { BASE_URL, url_name_3 } from "@/axios/Global";
export default {
  name: "",
  props: {
    config: {
      type: Object,
      default: () => {},
    },
    uploadCertUrl: {
      type: String,
      default: `${BASE_URL}/${url_name_3}/sys-fdfs-file/uploadFile`
    },
    extraData: Object,
    fileList: Array
  },
  data() {
    return {
    };
  },
  computed: {
    headers() {
      return {
        Authorization: `${sessionStorage.getItem("token")}`,
        Currorgcode: `${sessionStorage.getItem('deptId')}`
      };
    },
    configSet() {
      let obj = {
        multiple: false,
        limit: 999,
        limitSize: 10,
        acceptList: ".xls, .xlsx",
        showTip: true
      };
      return Object.assign(obj, this.config);
    },
  },
  watch: {},
  created() {},
  mounted() {
    console.log(this.uploadCertUrl)
  },
  methods: {
    handleRemove(file, fileList) {
      this.$emit("handleRemove", this.fileList);
    },
    uploadSuccess(response, file, fileList) {
      // console.log(response, file, fileList)
      this.$message({
        type: response.status ? 'success' : 'error',
        message: response.status ? '导入成功' : response.msg
      })
      this.$emit("uploadSuccess", response, this.fileList);
    },
    uploadError(err, file, fileList){
      console.log(err, file, fileList)
    },
    beforeRemove(file, fileList) {},
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < this.configSet.limitSize;
      if (!isLt2M) {
        this.$message.error(`上传文件大小不能超过${this.configSet.limitSize}MB`);
      }
      return isLt2M;
    },
    handleExceed() {
      this.$message.warning(
        `当前限制选择 ${this.configSet.limit} 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
  },
};
</script>

<style scoped lang='less'>
</style>